import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../lib_im/index.dart';
import '../../lib_network/http_helper.dart';

class SelFriendLogic extends GetxController with GetSingleTickerProviderStateMixin {
  var friends = <FriendModel>[].obs;
  var followings = <FriendModel>[].obs;
  var fans = <FriendModel>[].obs;
  var currentIndex = 0.obs;
  var searchController = TextEditingController();
  var searchKey = ''.obs;
  var shareMode = Get.arguments ?? false;

  late var tabController = TabController(length: 3, vsync: this);

  @override
  void onInit() {
    tabController.addListener(
      () {
        currentIndex(tabController.index);
      },
    );
    friendList();
    followList();
    fansList();
    super.onInit();
  }

  @override
  void onClose() {
    searchController.dispose();
    tabController.dispose();
    super.onClose();
  }

  // 粉丝列表
  fansList({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 20,
    int type = 0, //0:全部 1:在线
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/user-friend/get-fans',
      showLoading: showLoading,
      showToast: true,
      body: {
        'type': type,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['list'] is! List) return [];
    var list = List.generate(
      resp.result['list'].length,
      (index) => FriendModel.fromJson(resp.result['list'][index]),
    );
    fans.assignAll(list);
  }

  // 关注列表
  followList({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 20,
    int type = 0, //0:全部 1:在线
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/user-friend/get-attentions',
      showLoading: showLoading,
      showToast: true,
      body: {
        'type': type,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['list'] is! List) return [];
    var list = List.generate(
      resp.result['list'].length,
      (index) => FriendModel.fromJson(resp.result['list'][index]),
    );
    followings.assignAll(list);
  }

  // 好友列表
  friendList({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 20,
    int type = 0, //0:全部 1:在线
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/friend/friends',
      showLoading: showLoading,
      showToast: true,
      body: {
        'activeType': type,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['friends'] is! List) return [];
    var list = List.generate(
      resp.result['friends'].length,
      (index) => FriendModel.fromJson(resp.result['friends'][index]),
    );
    friends.assignAll(list);
  }
}
